#include <stddef.h>
#include <pthread.h>

#include "client_head.h"
#include "socket_client.h"
#include "socket_connect.h"

#include"packeage_pic_info.h"


unsigned char heart_reurrn_client[6] = { 0xef,0x03,0x04,0x00,0xef,0x03 };


int socket_client()
{

  int socket_fd=0;
  //创建一个客户端socket
  struct sockaddr_in server_addr;
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  if(inet_pton(AF_INET, IP_ADDRESS, &server_addr.sin_addr) == 0)
  {
    perror("Server IP Address Error:");
    exit(1);
  }
  server_addr.sin_port = htons(SERVER_PORT);

  //socket_connect(&server_addr);

  //创建多线程
  int err;
  pthread_t ntid;
  err = pthread_create(&ntid, NULL, (void *)socket_connect, &server_addr);
  if (err != 0)
  {
	  printf("can't create thread: %s\n", strerror(err));
  }
  while (1)
  {
	  sleep(10);
	  socket_fd=get_socket_fd();

	  //printf("socket_fd=%d\n",socket_fd);

	  if(socket_fd != -1)
	  {
	  	printf("The client automatically sends heartbeat signals %d\n",socket_fd);
	  	send(socket_fd, heart_reurrn_client, 6, 0);
      //printf("%d\n",recv(socket_fd, heart_reurrn_client, 6, 0));
		  send_pic(socket_fd);
	  }
  }

  return 0;
}
